package cn.sh.basic.controller;

import cn.sh.basic.domain.dto.SendSmsDto;
import cn.sh.basic.domain.dto.SendEmailDto;
import cn.sh.basic.service.IVerifyCodeService;
import cn.sh.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;


    @GetMapping("/image/{key}")
    public AjaxResult imageCode(@PathVariable("key") String key){
        try {
            return AjaxResult.me().setResultObj(verifyCodeService.imageCode(key));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMesssage("系统繁忙");
        }
    }


    /**
     * 发送短信验证码
     * @param dto
     * @return
     */
    @PostMapping("/sendSmsCode")
    public AjaxResult sendSmsCode(@RequestBody SendSmsDto dto){
        try {
            verifyCodeService.sendSmsCode(dto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMesssage(e.getMessage());
        }
    }


    /**
     * 发送邮件验证码
     * @param SendEmailDto
     * @return
     */
    @PostMapping("/sendEmailCode")
    public AjaxResult sendEmailCode(@RequestBody SendEmailDto emailDto){
        try {
            verifyCodeService.sendEmailCode(emailDto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMesssage(e.getMessage());
        }
    }
}
